/**
 * userRoutes.js
 * 用户相关API路由
 */

const express = require('express');
const userController = require('../controllers/userController');
const router = express.Router();

/**
 * 用户注册
 * POST /api/users/register
 * 请求体: { username, password, level?, dailyNewWordsLimit?, dailyReviewLimit? }
 * 响应: { success: boolean, message: string, userId?: number }
 */
router.post('/register', async (req, res) => {
  try {
    const { username, password, level, dailyNewWordsLimit, dailyReviewLimit } = req.body;
    
    if (!username || !password) {
      return res.status(400).json({ 
        success: false, 
        message: '用户名和密码不能为空' 
      });
    }
    
    const result = await userController.register(
      username, 
      password, 
      level, 
      dailyNewWordsLimit, 
      dailyReviewLimit
    );
    
    if (result.success) {
      res.status(201).json({
        success: true,
        message: '注册成功',
        userId: result.userId
      });
    } else {
      res.status(400).json({
        success: false,
        message: result.message
      });
    }
  } catch (error) {
    console.error('注册路由错误:', error);
    res.status(500).json({
      success: false,
      message: '服务器错误',
      error: error.message
    });
  }
});

/**
 * 用户登录
 * POST /api/users/login
 * 请求体: { username, password }
 * 响应: { success: boolean, message: string, user?: Object }
 */
router.post('/login', async (req, res) => {
  try {
    const { username, password } = req.body;
    
    if (!username || !password) {
      return res.status(400).json({
        success: false,
        message: '用户名和密码不能为空'
      });
    }
    
    const user = await userController.login(username, password);
    
    if (user) {
      res.status(200).json({
        success: true,
        message: '登录成功',
        user
      });
    } else {
      res.status(401).json({
        success: false,
        message: '用户名或密码错误'
      });
    }
  } catch (error) {
    console.error('登录路由错误:', error);
    res.status(500).json({
      success: false,
      message: '服务器错误',
      error: error.message
    });
  }
});

/**
 * 获取用户信息
 * GET /api/users/:username
 * 响应: { success: boolean, message: string, user?: Object }
 */
router.get('/:username', async (req, res) => {
  try {
    const { username } = req.params;
    
    const user = await userController.getUserInfo(username);
    
    if (user) {
      res.status(200).json({
        success: true,
        user
      });
    } else {
      res.status(404).json({
        success: false,
        message: '用户不存在'
      });
    }
  } catch (error) {
    console.error('获取用户信息路由错误:', error);
    res.status(500).json({
      success: false,
      message: '服务器错误',
      error: error.message
    });
  }
});

/**
 * 更新用户设置
 * PUT /api/users/:userId/settings
 * 请求体: { current_level?, daily_new_words_limit?, daily_review_limit? }
 * 响应: { success: boolean, message: string }
 */
router.put('/:userId/settings', async (req, res) => {
  try {
    const userId = parseInt(req.params.userId);
    const settings = req.body;
    
    if (isNaN(userId)) {
      return res.status(400).json({
        success: false,
        message: '无效的用户ID'
      });
    }
    
    const success = await userController.updateUserSettings(userId, settings);
    
    if (success) {
      res.status(200).json({
        success: true,
        message: '用户设置更新成功'
      });
    } else {
      res.status(400).json({
        success: false,
        message: '更新失败，可能没有提供有效的设置或用户不存在'
      });
    }
  } catch (error) {
    console.error('更新用户设置路由错误:', error);
    res.status(500).json({
      success: false,
      message: '服务器错误',
      error: error.message
    });
  }
});

module.exports = router;